package com.cssy.yd.basic.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.entity.BasicCurrencyDO;
import com.cssy.yd.basic.service.IBasicCurrencyService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/basic/currency")
public class CurrencyController extends BaseController<BasicCurrencyDO> {
	private static final String MANAGER = "/yd/basic/currency/CurrencyManager";// 列表页面
	private static final String EIDT = "/yd/basic/currency/CurrencyOperation";// 添加、修改、显示页面

	@Resource(name = "basicCurrencyService")
	private IBasicCurrencyService basicCurrencyService;

	

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);
		 if(scs.getOrderBy()==null||"".equals(scs.getOrderBy()))
			 scs.setOrderBy("dctime desc");
		 SearchResult result= basicCurrencyService.findListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);

	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			
		} else {
			BasicCurrencyDO bd =basicCurrencyService.findListById(id);
			model.addAttribute("currency", bd);
			
		}
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicCurrencyDO curAP, BindingResult result, Model model) {
		BodyMsg bodyMap = BodyMsg.getInstance();
		SearchConditions scs = new SearchConditions();
		scs.addCondition("currencyname", curAP.getCurrencyname(),SearchCondition.OP_EQUAL);
		SearchResult st = basicCurrencyService.findListByExample(scs);
		List<BasicCurrencyDO> bd = st.getResults();
		try {
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
				if (bd.size() > 0) {
					if(!curAP.getId().equals(bd.get(0).getId())){
						bodyMap.isFail();
						bodyMap.configWebInfor("您添加的币别已经存在，请重新添加");
						return bodyMap.modelMap;
					}
				}
				basicCurrencyService
						.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				
			} else {// 新增
				if (bd.size() > 0) {
					bodyMap.isFail();
					bodyMap.configWebInfor("您添加的币别已经存在，请重新添加");
					return bodyMap.modelMap;
				}
				BasicCurrencyDO cdo =  basicCurrencyService.save(curAP,
						SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId().toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;

	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			
			for(String field : fields){
				basicCurrencyService.delete(field, SessionUtil.getUser(request));
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
